/******************************************************************
 *
 *    Java Lib For JavaWeb, Powered By Shenzhen WEI YI KE JI .
 *
 *    Copyright (c) 2017-2027 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     com.theyeasy.dvmini.service.impl
 *
 *    Filename:    ProjectServiceImpl.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2017-2027
 *
 *    Company:     Theyeasy Telemedia Co.,Ltd
 *
 *    @author:     hm
 *
 *    @version:    1.0.0
 *
 *    Create at:   2017年10月22日 下午5:53:36
 *
 *    Revision:
 *
 *    2017年10月22日 下午5:53:36
 *        - first revision
 *
 *****************************************************************/
package com.theyeasy.dvmini.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.testng.util.Strings;

import com.theyeasy.dvmini.dao.ProjectDao;
import com.theyeasy.dvmini.model.Projects;
import com.theyeasy.dvmini.service.ProjectService;
import com.theyeasy.dvmini.util.PageResults;
import com.theyeasy.dvmini.util.StringUtil;

import tk.mybatis.mapper.entity.Example;

/**
 * @ClassName ProjectServiceImpl
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author hm
 * @Date 2017年10月22日 下午5:53:36
 * @version 1.0.0
 */
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    ProjectDao projectDao;
    
    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ProjectService#getProjectList(java.lang.String)
     */
    @Override
    public PageResults<Projects> getProjectList(Integer page,String keyword) {
        Example example=new Example(Projects.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleted", false);
        if(!Strings.isNullOrEmpty(keyword)){
            criteria.andLike("name", "%"+keyword+"%");
        }
        RowBounds row=new RowBounds((page-1)*10,10);
        example.setOrderByClause(" id desc ");
        List<Projects> list=projectDao.selectByExampleAndRowBounds(example, row);
       
        int total=projectDao.selectCountByExample(example);
        PageResults<Projects> pageresult=new PageResults<>();
        pageresult.setPage(page);
        pageresult.setPageSize(10);
        pageresult.setRows(list);
        pageresult.setTotal(total);
        return pageresult;
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ProjectService#addProject(java.lang.String, java.lang.String, java.lang.String, java.lang.Integer)
     */
    @Override
    public String addProject(String name, String lng, String lat, Integer templetid) {
         if(Strings.isNullOrEmpty(name))
             return "项目名称不能为空";
         Projects project=new Projects();
         project.setName(name);
         project.setPostertempletid((templetid==null ? 0 :templetid));
         project.setLat(lat);
         project.setLng(lng);
         project.setUpdatedtime(new Date());
         project.setDeleted(false);
        int row=projectDao.insertSelective(project);
        return row>0?"ok":"添加失败";
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ProjectService#updateProject(java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer)
     */
    @Override
    public String updateProject(Integer id, String name, String lng, String lat, Integer templetid) {
         if(StringUtil.isZearoOrNull(id))
             return "id不能为空";
         if(Strings.isNullOrEmpty(name))
             return "项目名称不能为空";
         if(StringUtil.isZearoOrNull(templetid))
             return "项目海报模板不能为空";
        Projects project=projectDao.selectByPrimaryKey(id);
        if(project==null)
            return "项目不存在";
        project.setName(name);
        project.setLat(lat);
        project.setLng(lng);
        project.setPostertempletid(templetid);
        int row=projectDao.updateByPrimaryKeySelective(project);
        return row>0?"ok":"修改失败";
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ProjectService#deleteProject(java.lang.Integer)
     */
    @Override
    public String deleteProject(Integer id) {
        if(StringUtil.isZearoOrNull(id))
            return "id不能为空";
        Projects project=projectDao.selectByPrimaryKey(id);
        if(project==null)
            return "项目不存在";
         project.setDeleted(true);
         int row=projectDao.updateByPrimaryKeySelective(project);
        return row>0?"ok":"删除失败";
    }

    /* (非 Javadoc)
     * Description:
     * @see com.theyeasy.dvmini.service.ProjectService#getProject(java.lang.Integer)
     */
    @Override
    public Projects getProject(Integer id) {
        if(StringUtil.isZearoOrNull(id))
            return null;
       return projectDao.selectByPrimaryKey(id);
    }
    
    @Override
    public List<Projects> getAllProjects() {
         Example example=new Example(Projects.class);
         example.createCriteria().andEqualTo("deleted",false);
         example.setOrderByClause(" id desc ");
        return projectDao.selectByExample(example);
    }

}
